package cn.mw.cmdb.template.controller;

import cn.mw.cmdb.template.entity.*;
import cn.mw.cmdb.template.entity.convert.MwHostGroupConvertDto;
import cn.mw.cmdb.template.entity.convert.MwTemplateConvertOne;
import cn.mw.cmdb.template.param.MwTemplateConfigurationParam;
import cn.mw.cmdb.template.param.TemplateParam;
import cn.mw.cmdb.template.service.MwTemplateConfigurationService;
import cn.mw.cmdb.view.ViewConvertContext;
import cn.mw.cmdb.view.ViewManager;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;

/**
 * @description 模板配置管理
 */
@RequestMapping("/template/configuration")
@Controller
@Tag(name = "模板配置管理")
@Slf4j
public class MwTemplateConfigurationController extends BaseApiService {

    private MessageSource messageSource;

    private MwTemplateConfigurationService templateConfigurationService;

    private ViewManager viewManager;

    private final String FILE = "file";
    private final String IMPORT_PARAM = "importParam";

    public MwTemplateConfigurationController(MessageSource messageSource , MwTemplateConfigurationService templateConfigurationService,ViewManager viewManager){
        this.messageSource = messageSource;
        this.templateConfigurationService = templateConfigurationService;
        this.viewManager = viewManager;
    }

    @PostMapping("/select")
    @ResponseBody
    @Operation(summary = "查询模板信息")
    public ResponseBase select(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        List<MwTemplateConfigurationDto> templateConfigurationDtos = templateConfigurationService.getTemplateInfoByServerId(param);
        Object pageRet = viewManager.getListViewObject(templateConfigurationDtos, MwTemplateConfigurationDto.class, param, new ViewConvertContext());
        return setResultSuccess(pageRet);
    }

    @PostMapping("/selectGroup")
    @ResponseBody
    @Operation(summary = "查询群组信息")
    public ResponseBase selectGroup(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        List<MwHostGroupConvertDto> hostGroup = templateConfigurationService.getHostGroup(param);
        return setResultSuccess(hostGroup);
    }

    @PostMapping("/add")
    @ResponseBody
    @Operation(summary = "新增模板信息")
    public ResponseBase add(@RequestBody TemplateParam templateParam) throws Exception {
        templateConfigurationService.createTemplateInfo(templateParam);
        return setResultSuccess();
    }

    @PostMapping("/edit")
    @ResponseBody
    @Operation(summary = "修改模板信息")
    public ResponseBase update(@RequestBody TemplateParam templateParam) throws Exception {
        templateConfigurationService.updateTemplateInfo(templateParam);
        return setResultSuccess();
    }

    @PostMapping("/remove")
    @ResponseBody
    @Operation(summary = "删除模板信息")
    public ResponseBase remove(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        templateConfigurationService.deleteTemplateInfo(param);
        return setResultSuccess();
    }


    @PostMapping("/selectById")
    @ResponseBody
    @Operation(summary = "根据模板ID查询模板信息")
    public ResponseBase selectById(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        MwTemplateConvertOne templateConvertOne = templateConfigurationService.getTemplateInfoById(param);
        return setResultSuccess(templateConvertOne);
    }

    @PostMapping("/getTemplateDetail")
    @ResponseBody
    @Operation(summary = "获取模板详情的信息")
    public ResponseBase getTemplateDetailInfo(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        Object detailInfo = templateConfigurationService.getTemplateDetailInfo(param);
        return setResultSuccess(detailInfo);
    }

    @PostMapping("/addTemplateDetail")
    @ResponseBody
    @Operation(summary = "添加模板详情的信息")
    public ResponseBase addTemplateDetailInfo(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        templateConfigurationService.addTemplateDetailInfo(param);
        return setResultSuccess();
    }

    @PostMapping("/updateTemplateDetail")
    @ResponseBody
    @Operation(summary = "修改模板详情的信息")
    public ResponseBase updateTemplateDetailInfo(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        templateConfigurationService.updateTemplateDetailInfo(param);
        return setResultSuccess();
    }

    @PostMapping("/deleteTemplateDetail")
    @ResponseBody
    @Operation(summary = "删除模板详情的信息")
    public ResponseBase deleteTemplateDetailInfo(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        templateConfigurationService.deleteTemplateDetailInfo(param);
        return setResultSuccess();
    }

    @PostMapping("/selectTemplateDetailById")
    @ResponseBody
    @Operation(summary = "根据模板详情ID获取详情数据")
    public ResponseBase selectTemplateDetailById(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        Object realData = templateConfigurationService.selectTemplateDetailById(param);
        return setResultSuccess(realData);
    }

    @PostMapping("/getMonitorItemType")
    @ResponseBody
    @Operation(summary = "获取监控项类型")
    public ResponseBase getMonitorItemTypeInfo() {
        List<DropDownDto> dropDownDtos = templateConfigurationService.getMonitorItemTypeInfo();
        return setResultSuccess(dropDownDtos);
    }

    @PostMapping("/getDetector")
    @ResponseBody
    @Operation(summary = "获取检测器")
    public ResponseBase getDetectorInfo(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        List<MwZabbixDetectorDto> detectorDtos = templateConfigurationService.getZabbixDetectorInfo(param);
        return setResultSuccess(detectorDtos);
    }

    @PostMapping("/export")
    @ResponseBody
    @Operation(summary = "导出模板信息")
    public void exportTemplate(@RequestBody MwTemplateConfigurationParam param, HttpServletResponse response) throws Exception {
        templateConfigurationService.exportTemplateData(param,response);
    }

    @PostMapping("/import")
    @ResponseBody
    @Operation(summary = "导入模板信息")
    public ResponseBase importTemplate(MultipartHttpServletRequest request) throws Exception {
        MultipartFile file = request.getFile(FILE);
        String json = request.getParameter(IMPORT_PARAM);
        MwTemplateConfigurationParam param = JSONObject.parseObject(json, MwTemplateConfigurationParam.class);
        templateConfigurationService.importTemplateData(param,file);
        return setResultSuccess();
    }

    @PostMapping("/getTriggerGroupInfo")
    @ResponseBody
    @Operation(summary = "获取触发器分组信息")
    public ResponseBase getTriggerGroupInfo(@RequestBody MwTemplateConfigurationParam param) throws Exception {
        List<TriggerGroupDto> triggerGroups = templateConfigurationService.getTriggerGroupInfo(param);
        return setResultSuccess(triggerGroups);
    }

    @PostMapping("/getFunctionDropDown")
    @ResponseBody
    @Operation(summary = "获取表达式下拉信息")
    public ResponseBase getExpressionDropDownInfo() throws Exception {
        List<FunctionClassifyDto> functionClassifyDtoList = templateConfigurationService.getZabbixFunctionInfo();
        return setResultSuccess(functionClassifyDtoList);
    }
}
